import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store/index.js";
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录"
    },
    component: () => import("@/views/login/index.vue")
  },
  {
    path: "/",
    name: "viewPort",
    meta: {
      title: "视口"
    },
    redirect: "/index",
    component: () => import("@/views/index.vue"),
    children: [
      {
        path: "/index",
        name: "index",
        meta: {
          title: "首页"
        },
        component: () => import("@/views/home/index.vue")
      }
    ]
  },
  {
    path: "/educationIndex",
    name: "educationIndex",
    meta: {
      title: "教育局视口"
    },
    redirect: "/educationIndex/eduHome",
    component: () => import("@/views/education-index/index.vue"),
    children: [
      {
        path: "eduHome",
        name: "eduHome",
        meta: {
          keepAlive: true,
          title: "教育局首页"
        },
        component: () => import("@/views/edu-home/index.vue")
      },
      {
        path: "eduWorkAction",
        name: "eduWorkAction",
        meta: {
          title: "教育局工作动态"
        },
        component: () => import("@/views/edu-workAction/index.vue")
      },
      {
        path: "eduWorkAction-eduArticleDetail/:id",
        name: "eduWorkAction-eduArticleDetail",
        meta: {
          title: "文章详情"
        },
        component: () => import("@/views/edu-articleDetail/index.vue")
      },
      {
        path: "eduLeader",
        name: "eduLeader",
        meta: {
          title: "组织机构"
        },
        component: () => import("@/views/edu-leader/index.vue")
      }
    ]
  },
  {
    path: "/manage",
    name: "manage",
    meta: {
      title: "管理视口"
    },
    redirect: "/manage/userPermisition",
    component: () => import("@/views/manage/index.vue"),
    beforeEnter: (to, from, next) => {
      console.log(to, from, store);
      // 去登录页，下面不需要执行
      if (to.path === "/login") {
        return next();
      }
      if (!store.state.userInfo.zh_id) {
        //没有登录，去跳转登录页
        if (to.path === "/login") {
          return next();
        } else {
          Vue.prototype.$message.error("尚未登录，请先登录");
          return next({ path: "/login" });
        }
      } else {
        next();
      }
    },
    children: [
      {
        path: "userPermisition",
        name: "userPermisition",
        meta: {
          title: "角色权限"
        },
        component: () => import("@/views/user-permisition/index.vue")
      },
      {
        path: "pageSet",
        name: "pageSet",
        meta: {
          title: "页面设置"
        },
        component: () => import("@/views/page-set/index.vue")
      },
      {
        path: "linkSet",
        name: "linkSet",
        meta: {
          title: "链接管理"
        },
        component: () => import("@/views/link-set/index.vue")
      },
      {
        path: "userSet",
        name: "userSet",
        meta: {
          title: "用户管理"
        },
        component: () => import("@/views/user-set/index.vue")
      },
      {
        path: "sysSet",
        name: "sysSet",
        meta: {
          title: "系统设置"
        },
        component: () => import("@/views/sys-set/index.vue")
      }
      // {
      //   path: "editPassword",
      //   name: "editPassword",
      //   meta: {
      //     title: "修改密码"
      //   },
      //   component: () => import("@/views/edit-password/index.vue")
      // }
    ]
  },
  {
    path: "*",
    redirect: "/404"
  },
  {
    path: "/404",
    name: "not Found",
    component: () => import("../views/404.vue")
  }
];
store.commit("removeState");
window.onbeforeunload = function() {
  store.commit("saveState");
};
const router = new VueRouter({
  routes
});

export default router;
